package cn.engage.ekcc.web.controller;

import cn.engage.ekcc.domain.Product;
import cn.engage.ekcc.service.IProductBuyService;
import cn.engage.ekcc.domain.ProductBuy;
import cn.engage.ekcc.query.ProductBuyQuery;
import cn.engage.ekcc.result.JsonResult;
import cn.engage.ekcc.result.PageList;
import cn.engage.ekcc.vo.BuyVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/productBuy")
public class ProductBuyController {

    @Autowired
    public IProductBuyService productBuyService;
    /* 采购 */
    @RequestMapping(value = "/cg",method = RequestMethod.POST)
    public JsonResult CG(@RequestBody BuyVo buyVo){
        productBuyService.CG(buyVo);
        return JsonResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@Valid @RequestBody ProductBuy productBuy){
        if(productBuy.getId()!=null){
            productBuyService.updateById(productBuy);
        }else{
            productBuyService.insert(productBuy);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        productBuyService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(productBuyService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(productBuyService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ProductBuyQuery query){
        Page<ProductBuy> page = new Page<ProductBuy>(query.getPage(),query.getRows());
        page = productBuyService.selectPage(page);
        return JsonResult.success(new PageList<ProductBuy>(page.getTotal(),page.getRecords()));
    }
}
